package org.feluma.sgci.util;

import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.*;

import java.io.*;
import java.util.*;
import java.sql.*;

import javax.servlet.*;
import javax.servlet.http.*;

import org.feluma.sgci.controle.Transporte;
import org.feluma.sgci.util.Conexao;

public class GerarRelatorio extends HttpServlet{

	 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 String caminho = "";   
		 try {
	            Connection conn = Conexao.getConexaoMssql();
	        //    if(Transporte.getLocal().equalsIgnoreCase("servidor")){
	               caminho = "/usr/local/apache-tomcat-6.0.24/webapps/SGCI/relatorio/";
	           // 	caminho =  File.separator + "usr" + File.separator + "local" + File.separator + "apache-tomcat-6.0.24" + File.separator + "webapps" + File.separator + "SGCI" + File.separator + "relatorio" + File.separator;
	        //    } else if(Transporte.getLocal().equalsIgnoreCase("wemerson-pc")){
	      //    	caminho = "C:" + File.separator + "Users" + File.separator + "wemerson" + File.separator + "workspace" + File.separator + "SGCI" + File.separator + "WebContent" + File.separator + "relatorio" + File.separator;
	      //      }else{
	       //     	caminho = "C:" + File.separator + "Users" + File.separator + "wemersonm" + File.separator + "workspace" + File.separator + "SGCI" + File.separator + "WebContent" + File.separator + "relatorio" + File.separator;
	      //      }
	          	String relatorio = request.getParameter("relatorio");
	          	String codCurso = request.getParameter("curso");
	            Integer processo = (Integer.parseInt(request.getParameter("processo")));
	            Integer codEscola = (Integer.parseInt(request.getParameter("hiddenCodEscola")));
	            String dataInicio =  ConverteData.converteDataFormatoVisual(request.getParameter("dataInicio"));
	            String dataFim =  ConverteData.converteDataFormatoVisual(request.getParameter("dataFim"));
	            
	            HashMap parametros = new HashMap();	            
	            if(relatorio.equals("1")){	            	
	            	caminho +=  "geralAlunoCurso.jasper"; 	 	            
	            	parametros.put("dataInicio",dataInicio);
	            	parametros.put("dataFim", dataFim);
	            	parametros.put("codEscola", codEscola);
	            	parametros.put("processo", processo);
	            } else if(relatorio.equals("2")){	            	
	            	caminho +=  "sinteticoAlunoCurso.jasper"; 	 	            
	            	parametros.put("dataInicio",dataInicio);
	            	parametros.put("dataFim", dataFim);
	            	parametros.put("codEscola", codEscola);	    
	            	parametros.put("processo", processo);
	            } else if(relatorio.equals("3")){	            	
	            	caminho +=  "listaPresenca.jasper"; 	 	            
	            	parametros.put("dataInicio",dataInicio);
	            	parametros.put("dataFim", dataFim);
	            	parametros.put("codEscola", codEscola);	    
	            	parametros.put("processo", processo);
	            	parametros.put("codCurso", Integer.parseInt(codCurso));
	            }
	                
	            JasperPrint prt = JasperFillManager.fillReport(caminho,parametros,conn);
	            byte[] report = new byte[0];
	            ServletOutputStream sos = response.getOutputStream();
	            report = JasperExportManager.exportReportToPdf(prt);
	            response.setContentType("application/pdf");
	            sos.write(report,0,report.length);

	            sos.flush();
	            sos.close();
	            conn.close();

	        }catch(IOException eIO) {
	            eIO.printStackTrace();
	        }catch(SQLException eSQL) {
	            eSQL.printStackTrace();
	        }catch(Exception e) {
	            e.printStackTrace();
	        }
	    }
	}


